const Koa = require('koa')
const app = new Koa()

const proxy = require('koa2-proxy-middleware');
const bodyparser = require('koa-bodyparser');

const self_config = require('./config');

const options = {
    targets: {
        '/api/(.*)': {
            target: self_config.redit_url,
            changeOrigin: true,
            pathRewrite: {
                '/api': '',
            }
        },
    }
}

app.use(proxy(options));

app.use(require('koa-static')(__dirname + '/views/www'))

app.use(bodyparser({
    enableTypes:['json', 'form', 'text']
}));
// logger
app.use(async (ctx, next) => {
    const start = new Date()
    await next()
    const ms = new Date() - start
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

module.exports = app
